package com.qxsx.midi.activity.exam;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.activity.home.MusicDetailsActivity;
import com.qxsx.midi.activity.me.MeKejiantActivity;
import com.qxsx.midi.adapter.TimeChooseWheelAdapter;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.config.AppManager;
import com.qxsx.midi.utils.CustomProgress;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.utils.callBackJsonArray;
import com.qxsx.midi.wheel.WheelView;
import com.qxsx.midi.wheel.WheelViewg;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Arrays;
import java.util.List;

/**
 * Created by Zrj on 2015/9/10.
 * 报名考试
 */
public class ExamEnrollActivtiy extends BaseActivity implements View.OnClickListener {
    private CustomProgress customProgress;

    private RelativeLayout rl_guide;

    private Button btn_enroll;
    Dialog dialog;
    private TextView tv_year, tv_month, tv_day;
    private TextView tv_address, tv_subject, tv_grade, tv_price;
    private TextView tv_star_hour, tv_star_minute, tv_end_hour, tv_end_minute;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exam_enroll);
        setTitle("报名");
        setNavBtn(R.mipmap.back, 0);


        findById();
    }

    @Override
    protected void onResume() {
        super.onResume();
        exammoney();
        String exam_year = (String) SPUtils.get(AppContext.getContext(), "exam_year", "");
        String exam_month = (String) SPUtils.get(AppContext.getContext(), "exam_month", "");
        String exam_day = (String) SPUtils.get(AppContext.getContext(), "exam_day", "");
        String star_hour = (String) SPUtils.get(AppContext.getContext(), "star_hour", "");
        String star_minute = (String) SPUtils.get(AppContext.getContext(), "star_minute", "");
        String end_hour = (String) SPUtils.get(AppContext.getContext(), "end_hour", "");
        String end_minute = (String) SPUtils.get(AppContext.getContext(), "end_minute", "");
        String subject_name = (String) SPUtils.get(AppContext.getContext(), "subject_name", "");
        String choos_address = (String) SPUtils.get(AppContext.getContext(), "choos_address", "");
        String grade_value = (String) SPUtils.get(AppContext.getContext(), "grade_value", "");


        tv_year.setText(exam_year);
        tv_month.setText(exam_month);
        tv_day.setText(exam_day);
        tv_address.setText(choos_address);
        tv_subject.setText(subject_name);
        tv_grade.setText(grade_value);
        tv_star_hour.setText(star_hour);
        tv_star_minute.setText(star_minute);
        tv_end_hour.setText(end_hour);
        tv_end_minute.setText(end_minute);

    }


    private void exammoney() {
        customProgress = CustomProgress.show(ExamEnrollActivtiy.this, "请稍后....", true, null);

        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        String apply_major_id = (String) SPUtils.get(AppContext.getContext(), "apply_major_id", "2172405d-17bb-5f40-da92-cf9cc285a980");
        String apply_grade_value = (String) SPUtils.get(AppContext.getContext(), "apply_grade_value", "1");


        try {
            object.put("major_id", apply_major_id);
            object.put("grade_num", apply_grade_value);
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        MyNet.doPostJsonArray("508", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                if (array == null) {
                    return;
                } else if (code.toString().equals("200")) {
                    try {
                        JSONObject object1 = array.getJSONObject(0);
                        String cost = object1.getString("cost");
                        tv_price.setText(cost);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                customProgress.dismiss();
            }
        });


    }

    private void examapply() {
        customProgress = CustomProgress.show(ExamEnrollActivtiy.this, "请稍后....", true, null);

        String user_id = (String) SPUtils.get(AppContext.getContext(), "user_id", "");
        String exam_site_id = (String) SPUtils.get(AppContext.getContext(), "exam_site_id", "");
        String apply_major_id = (String) SPUtils.get(AppContext.getContext(), "apply_major_id", "");
        String apply_grade_value = (String) SPUtils.get(AppContext.getContext(), "apply_grade_value", "");
        String exam_id = (String) SPUtils.get(AppContext.getContext(), "exam_id", "");

        String exam_year = (String) SPUtils.get(AppContext.getContext(), "exam_year", "");
        String exam_month = (String) SPUtils.get(AppContext.getContext(), "exam_month", "");
        String exam_day = (String) SPUtils.get(AppContext.getContext(), "exam_day", "");
        String star_hour = (String) SPUtils.get(AppContext.getContext(), "star_hour", "");
        String star_minute = (String) SPUtils.get(AppContext.getContext(), "star_minute", "");
        //日期
        String date = exam_year + "-" + exam_month + "-" + exam_day;
        //时间
        String time = star_hour + ":" + star_minute;


        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();

        try {
            object.put("user_id", user_id);
            object.put("exam_site_id", exam_site_id);
            object.put("major_id", apply_major_id);
            object.put("grade_num", apply_grade_value);
            object.put("exam_time", date + " " + time);
            object.put("site_date", date);
            object.put("site_time_id", exam_id);
            object.put("cost", tv_price.getText().toString().trim());
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        MyNet.doPostJsonArray("501", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                Log.w("code", code + "");
                Log.w("message", message + "");
                Log.w("array", array + "");
                if (code.toString().equals("428")) {
                    ShowToast("该时间段已被占用，请您重新选择考试时间");
                    customProgress.dismiss();
                    return;
                }
                customProgress.dismiss();
                startActivity(new Intent(AppContext.getContext(), ExamMyListActivity.class));
                AppManager.getAppManager().finishAllActivity();
                finish();

            }
        });


    }


    private void findById() {
        tv_year = (TextView) findViewById(R.id.tv_year);
        tv_month = (TextView) findViewById(R.id.tv_month);
        tv_day = (TextView) findViewById(R.id.tv_day);
        tv_address = (TextView) findViewById(R.id.tv_address);
        tv_subject = (TextView) findViewById(R.id.tv_subject);
        tv_grade = (TextView) findViewById(R.id.tv_grade);
        tv_price = (TextView) findViewById(R.id.tv_price);
        tv_star_hour = (TextView) findViewById(R.id.tv_star_hour);
        tv_star_minute = (TextView) findViewById(R.id.tv_star_minute);
        tv_end_hour = (TextView) findViewById(R.id.tv_end_hour);
        tv_end_minute = (TextView) findViewById(R.id.tv_end_minute);
        rl_guide = (RelativeLayout) findViewById(R.id.rl_guide);
        rl_guide.setOnClickListener(this);
        btn_enroll = (Button) findViewById(R.id.btn_enroll);
        btn_enroll.setOnClickListener(this);

    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        switch (component) {
            case LEFT:
                finish();
                break;
        }
    }

    private void diagobtn() {
        LayoutInflater layout = LayoutInflater.from(AppContext.getContext());
        View view = layout.inflate(R.layout.dialog_buy_finish, null);
        dialog = new AlertDialog.Builder(ExamEnrollActivtiy.this).create();
        Button btn_see = (Button) view.findViewById(R.id.btn_see);
        Button btn_clance = (Button) view.findViewById(R.id.btn_cancel);
        TextView tv_zhifu = (TextView) view.findViewById(R.id.tv_zhifu);
        tv_zhifu.setText("是否确定报名？");
        btn_see.setText("确定");
        btn_clance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        btn_see.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                examapply();

                dialog.dismiss();
            }
        });
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int x = dm.widthPixels;// 屏幕的宽
        int y = dm.heightPixels;// 屏幕的高
        dialog = new Dialog(ExamEnrollActivtiy.this, R.style.dialog_finish);
        dialog.getWindow().setLayout((int) (x / 2.2), (int) (y / 1.8));
        dialog.show();
        dialog.setContentView(view);
        dialog.setCancelable(false);


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_enroll:  //点击报名按钮
                diagobtn();

                break;
            case R.id.rl_guide:
                startActivity(new Intent(AppContext.getContext(), ExamProcessActivity.class));


                break;

            default:
                break;


        }
    }
}
